package com.sunxueqi.project.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunxueqi.common.constant.CfnConstants;
import com.sunxueqi.common.constant.StatusConstants;
import com.sunxueqi.common.utils.ServletUtils;
import com.sunxueqi.framework.security.LoginUser;
import com.sunxueqi.framework.security.service.LoginService;
import com.sunxueqi.framework.security.service.TokenService;
import com.sunxueqi.framework.web.vo.ResponseData;
import com.sunxueqi.project.system.service.IMenuService;
import com.sunxueqi.project.system.service.IRoleService;
import com.sunxueqi.project.system.vo.Menu;
import com.sunxueqi.project.system.vo.User;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import io.swagger.annotations.Api;

@Api(tags="登录相关接口")
@RestController
public class LoginController {

	@Autowired
	private LoginService loginService;
	
	@Autowired
    private TokenService tokenService;
	
	@Autowired
	private IMenuService menuService;
	
	@Autowired
	private IRoleService roleService;
	
	// private static final Logger log = LoggerFactory.getLogger(LoginController.class);
	Log log = LogFactory.get();
	/**
	 * 用户登录接口
	 * 
	 * @param username 用户名
	 * @param password 密码
	 * @param code     验证码
	 * @param uuid     唯一标识
	 * @return 响应结果
	 */
	@PostMapping("/login")
	public ResponseData login(String username, String password, String code, String uuid) {
		
		String token = loginService.login(username, password, code, uuid);
		Map<String, String> map = new HashMap<String, String>();
		map.put(CfnConstants.TOKEN, token);
		ResponseData respData = ResponseData.success("data", map);
		return respData;
	}
	
	/**
	 * 获取用户信息
	 * @return
	 */
	@GetMapping("/getUserInfo")
	public ResponseData getInfo() {
		
		log.info("这是{}log", Level.INFO);
		
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if(loginUser == null) {
			return ResponseData.error(StatusConstants.TOKEN_TIMEOUT, "连接超时");
		}
		User user = loginUser.getUser();
		// 角色集合
		List<String> roles = roleService.getRolesByUser(user);
		// 权限集合
		List<String> permissions = menuService.selectPermissionsByUser(user);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", user);
		map.put("roles", roles);
		map.put("permissions", permissions);
		
		ResponseData responseData = ResponseData.success("data", map);
		return responseData;
	}
	
	/**
	 * 获取当前用户的路由信息
	 * @return
	 */
	@GetMapping("/getRouters")
	public ResponseData getRouters() {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		// 用户信息
		User user = loginUser.getUser();
		List<Menu> menus = menuService.selectMenuTreeByUserId(user.getId());
		return ResponseData.success(menuService.buildMenus(menus));
//		return ResponseData.success(menuService.buildMenusForNaiveUI(menus));
	}
	
}
